<?php
namespace Home\Controller;
use Think\Controller;
// var_dump($_SESSION);

class LoginController extends Controller {

    
    public function index(){
        $this->display();
    }

    //用户的登陆操作
    public function loginin(){
    // echo 111111;    die;
        //创建用户模型
        // var_dump($_POST);
        $user = M('mvp_user'); 
        $user->create();
        //查找数据库
        $username = I('post.username');
        $password = md5(I('post.password'));
        $where['username']=array('eq',$username);
        $where['password'] = array('eq',$password);
        $where['status'] = array('eq',0);
       
        $infos = $user->where($where)->find();
        if($infos){
       		 $this->error('您已被禁止登录订餐', U('Home/Index/index'),3);
       		 return false;
        }
     	$info = $user->where("username = '{$username}' and password='{$password}'")->find();
        
        // echo $user->_sql();die;
        //var_dump($info);
        //创建info表的模型
        $userinfo=M('mvp_info');
        $_POST['time'] = time();//登陆时间
        $_POST['ip'] = ip2long($_SERVER['REMOTE_ADDR']);//登录者的IP
        
        $uid = $info['id'];
         // var_dump($uid);
         // var_dump($_POST);
        $userinfo->create();
        $con = $userinfo->where("uid = ".$uid)->save();
        // echo $userinfo->_sql();
        // var_dump($con);die;
        if(!empty($info) && $con>0){

            $_SESSION['username'] = $info['username'];
            $_SESSION['id'] = $info['id'];

            $this->success('恭喜您登陆成功', U('Home/Index/index'),3);
        }else{
            $this->error('用户名或密码错误', U('Home/Index/index'),3);
        }
    }
    public function logout(){
       setcookie('PHPSESSID','',time()-1,'/');
       unset($_SESSION['username']);
       $arr = session_destroy(); 
         if($arr){
            $this->success('退出成功', U('Home/Index/index'),3);
        }else{
            $this->error('退出失败', U('Home/Index/index'),3);
        }
    }
    
}